package com.ruoyi.project.onlineExperimentOrder.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.onlineExperimentOrder.domain.OnlineExperimentOrder;
import com.ruoyi.project.onlineExperimentOrder.mapper.OnlineExperimentOrderMapper;
import com.ruoyi.project.onlineExperimentOrder.service.OnlineExperimentOrderService;
import com.ruoyi.project.online_experiment.domain.OnlineExperiment;
import com.ruoyi.project.online_experiment.mapper.OnlineExperimentMapper;
import com.ruoyi.project.online_experimentStu.domain.OnlineExperimentStu;
import com.ruoyi.project.online_experimentStu.domain.OnlineExperimentTea;
import com.ruoyi.project.online_experimentStu.domain.Student;
import com.ruoyi.project.online_experimentStu.mapper.OnlineExperimentStuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName OnlineExperimentOrderImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-07-27 15:54
 **/
@Service
public class OnlineExperimentOrderImpl implements OnlineExperimentOrderService {

    @Autowired
    private OnlineExperimentOrderMapper onlineExperimentOrderMapper;

    @Autowired
    private OnlineExperimentMapper onlineExperimentMapper;

    @Autowired
    private OnlineExperimentStuMapper onlineExperimentStuMapper;
    /**
     *
     *  学生预约虚拟实验方法
     * @param onlineExperimentOrder  实体对象
     * @return
     */
    @Override
    public boolean onlineExperimentOrder(OnlineExperimentOrder onlineExperimentOrder) {
        return onlineExperimentOrderMapper.onlineExperimentOrder(onlineExperimentOrder);
    }

    /**
     * 学生更新预约虚拟实验方法
     * @param onlineExperimentOrder
     * @return
     */
    @Override
    public boolean updateOrder(OnlineExperimentOrder onlineExperimentOrder) {
        return onlineExperimentOrderMapper.updateOrder(onlineExperimentOrder);
    }


    /**
     * 判断学生是否已经预约
     *       若已经预约，再次预约则是更新预约
     * @param onlineExperimentOrder
     * @return
     */
    @Override
    public OnlineExperimentOrder getOnlineExperimentOrder(OnlineExperimentOrder onlineExperimentOrder) {
        return onlineExperimentOrderMapper.getOnlineExperimentOrder(onlineExperimentOrder);
    }

    /**
     * 获取预约的列表
     * @param onlineExperimentOrder
     * @return
     */
    @Override
    public List<OnlineExperimentOrder> getOrderList(OnlineExperimentOrder onlineExperimentOrder) {
        return onlineExperimentOrderMapper.getOrderList(onlineExperimentOrder);
    }

    /**
     * 教师审批操作
     * @param onlineExperimentOrder
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean approve(OnlineExperimentOrder onlineExperimentOrder) {
        /**
         * 判断是拒绝还是通过
         */
        int status=onlineExperimentOrder.getStatus();
        //即拒绝预约操作
        if(status==3){
            /**
             * 根据用户id和虚拟项目id查询预约信息
             */
            OnlineExperimentOrder onlineExperimentOrder2=onlineExperimentOrderMapper.getOnlineExperimentOrder(onlineExperimentOrder);
            /**
             * 判断是否有对应的项目信息
             */
            if(!StringUtils.isNull(onlineExperimentOrder2.getExperimentTeaId()) || onlineExperimentOrder2.getExperimentTeaId()!=null){
                /**
                 * 删除发布实验信息
                 */
                onlineExperimentStuMapper.deleteOnlineExperimentTea(onlineExperimentOrder2.getExperimentTeaId());
                onlineExperimentStuMapper.deleteOnlineExperimentStu(onlineExperimentOrder2.getExperimentTeaId());
                onlineExperimentOrder.setExperimentTeaId(null);
            }
            return onlineExperimentOrderMapper.approve(onlineExperimentOrder);
        }else{
            /**
             * 获取项目信息
             */
            OnlineExperiment onlineExperiment=onlineExperimentMapper.selectOnlineExperimentById((long) onlineExperimentOrder.getOnlineExperimentId());
            /**
             * 获取预约信息
             */
            OnlineExperimentOrder onlineExperimentOrder3=onlineExperimentOrderMapper.getOnlineExperimentOrder(onlineExperimentOrder);
            /**
             * 生成实验对应的唯一标识
             */
            String uuid=UUID.randomUUID().toString();
            /**
             * 创建教师布置实验对象并赋值
             */
            OnlineExperimentTea onlineExperimentTea=new OnlineExperimentTea();
            onlineExperimentTea.setExperimentTeaId(uuid);
            onlineExperimentTea.setExperimentTeaName(onlineExperiment.getOnlineExperimentName());
            onlineExperimentTea.setExperimentTeaBeginTime(onlineExperimentOrder3.getBeginTime());
            onlineExperimentTea.setExperimentTeaEndTime(onlineExperimentOrder3.getEndTime());
            onlineExperimentTea.setOnlineExperimentId(onlineExperimentOrder.getOnlineExperimentId());
            onlineExperimentTea.setUserId(onlineExperiment.getAssignTeacher());
            /**
             * 进行插入操作
             */
            onlineExperimentStuMapper.experimentTest(onlineExperimentTea);
            /**
             * 插入成绩表，即实验和学会说呢过对应的表
             */
            Student student=new Student();
            student.setStudentId(onlineExperimentOrder.getUserId());
            student.setStudentName(onlineExperimentOrder.getUserName());
            student.setExperimentTeaId(uuid);
            /**
             * 执行插入操作
             */
            onlineExperimentStuMapper.correspondingPerson(student);

            /**
             * 将对应实验id放入预约表里
             */
            onlineExperimentOrder.setExperimentTeaId(uuid);
            return onlineExperimentOrderMapper.approve(onlineExperimentOrder);
        }
    }

    /**
     * 學生撤銷預約
     * @param onlineExperimentOrder
     * @return
     */
    @Override
    public boolean revocation(OnlineExperimentOrder onlineExperimentOrder) {
        return onlineExperimentOrderMapper.revocation(onlineExperimentOrder);
    }
}
